#!/bin/bash

digits=auto
mindigits=3

if [ "$digits" = "auto" ] ; then
	num=$(quilt series | wc -l)
	digits=$(expr length "$num")
fi

if [ $digits -le $mindigits ] ; then
	digits=$mindigits
fi

quilt series | (
	num=0
	while read pname ; do
		num=$(expr $num + 1)
		bpname=$(echo "$pname" | sed 's/^[0-9]\+-\(.*\)/\1/')
		if [ $digits = 0 ] ; then
			npname="$bpname"
		else
			npname="$(printf "%.${digits}d-%s" $num "$bpname")"
		fi
		if [ "$pname" != "$npname" ] ; then
			quilt rename -P "$pname" "$npname"
		fi
	done
)
